﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CAGFIP.DataAccess.DataSetTableAdapters;
using System.Xml;


namespace XML
{
    class ExportXML
    {
        public bool ExportXml(string file)
        {
            PerguntaTableAdapter perguntaTA = new PerguntaTableAdapter();
            DataTable dt = new DataTable();
            dt = perguntaTA.GetData();

            XmlDocument xmlDoc = new XmlDocument();
            string str = file;
            try
            {
                xmlDoc.Load(str);
            }
            catch {
                XmlTextWriter xmlWriter = new XmlTextWriter(str, System.Text.Encoding.UTF8);
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                xmlWriter.WriteStartElement("Perguntas");
                xmlWriter.Close();
                xmlDoc.Load(str);
            }

            foreach (DataRow dr in dt.Rows)
            {

                XmlNode root = xmlDoc.DocumentElement;
                XmlElement childNode = xmlDoc.CreateElement("Pergunta");
                root.AppendChild(childNode);

                XmlElement subchild1 = xmlDoc.CreateElement("Enunciado");
                XmlText textnode1 = xmlDoc.CreateTextNode("Enunciado");
                textnode1.Value = (string) dr["Enunciado"];
                subchild1.AppendChild(textnode1);

                XmlElement subchild2 = xmlDoc.CreateElement("Alt_correta");
                XmlText textnode2 = xmlDoc.CreateTextNode("Alt_correta");
                textnode2.Value = (string) dr["AlternativaCorreta"];
                subchild2.AppendChild(textnode2);

                XmlElement subchild3 = xmlDoc.CreateElement("Alt_incorreta1");
                XmlText textnode3 = xmlDoc.CreateTextNode("Alt_incorreta1");
                textnode3.Value = (string)dr["AlternativaIncorreta1"];
                subchild3.AppendChild(textnode3);
                
                XmlElement subchild4 = xmlDoc.CreateElement("Alt_incorreta2");
                XmlText textnode4 = xmlDoc.CreateTextNode("Alt_incorreta2");
                textnode4.Value = (string)dr["AlternativaIncorreta2"];
                subchild4.AppendChild(textnode4);
                
                XmlElement subchild5 = xmlDoc.CreateElement("Alt_incorreta3");
                XmlText textnode5 = xmlDoc.CreateTextNode("Alt_incorreta3");
                textnode5.Value = (string)dr["AlternativaIncorreta3"];
                subchild5.AppendChild(textnode5);
                
                XmlElement subchild6 = xmlDoc.CreateElement("Alt_incorreta4");
                XmlText textnode6 = xmlDoc.CreateTextNode("Alt_incorreta4");
                textnode6.Value = (string)dr["AlternativaIncorreta4"];
                subchild6.AppendChild(textnode6);
                
                XmlElement subchild7 = xmlDoc.CreateElement("Id_assunto");
                XmlText textnode7 = xmlDoc.CreateTextNode("Id_assunto");
                int k = (int) dr["Assunto"];
                textnode7.Value = k.ToString();
                subchild7.AppendChild(textnode7);

                childNode.AppendChild(subchild1);
                childNode.AppendChild(subchild2);
                childNode.AppendChild(subchild3);
                childNode.AppendChild(subchild4);
                childNode.AppendChild(subchild5);
                childNode.AppendChild(subchild6);
                childNode.AppendChild(subchild7);

            }

            xmlDoc.Save(str);
            return true;
        }
    }
}
